OpenLayers.Control.Click = OpenLayers.Class(OpenLayers.Control, {                
    defaultHandlerOptions: {
        'single': true,
        'double': false,
        'pixelTolerance': 0,
        'stopSingle': false,
        'stopDouble': false,
        'map' : ''
    },
    initialize: function(options) {
        this.handlerOptions = OpenLayers.Util.extend(
            {}, this.defaultHandlerOptions
        );
        OpenLayers.Control.prototype.initialize.apply(
            this, arguments
        ); 
        this.handler = new OpenLayers.Handler.Click(
            this, {
                'click': this.trigger
            }, this.handlerOptions
        );
        $('available_layers').observe('change', function(){$('map_props').submit()});
    }, 
    trigger: function(e) {
        var lonlat = this.map.getLonLatFromViewPortPx(e.xy);
        element = $('ajax_url');
        var params = 'x=' + lonlat.lon + '&y='+ lonlat.lat+'&';
        $$('#map_props select').each(function(element){
            params +=  element.readAttribute('name') + '=' + element.value + '&';
        }.bind(params));
        $$('#map_props input').each(function(element){
           if(element.readAttribute('type') == 'checkbox')
            { 
                if(element.checked)
                    params +=  element.readAttribute('name') + '=' + element.value.gsub("#", "") + '&';
            }
            else
                params +=  element.readAttribute('name') + '=' + element.value.gsub("#", "") + '&';
        }.bind(params));
        new Ajax.Request(element.href, {
            method: "post",
            parameters : params,
            onSuccess: this.showResults.bind(this),
            onLoading : function(){$('overall').addClassName('loading-image'); $('map').addClassName('loading-image');}
        });
    },
    showResults: function(response)
    {
        $('overall').removeClassName('loading-image');
        $('map').removeClassName('loading-image');
        arr_response = response.responseJSON;
        if(arr_response !== null)
        {
            $('result_region').update(arr_response['meta']['language_result']+' '+ arr_response['meta']['region']);
            if(arr_response['meta'].results > 0)
            {
                insert_string = '<ol>';
                for(cnt=0; cnt<arr_response['meta'].results; ++cnt)
                    insert_string += '<li><a href="index.php?input=map&m_id='+arr_response['data'][cnt].dt_id+'" title="'+arr_response['data'][cnt].e_name+'">' +arr_response['data'][cnt].dt_titel+'</a>';
                insert_string += '</ol>';
            }
            else
               insert_string = '<p>' + arr_response['meta']['nodata'] + '</p>'; 
        }
        else
             insert_string = 'Sorry, an error occurred';
        //$('available_maps').update(insert_string).up().show().down().addClassName('available_maps_update');
        $('available_maps').update(insert_string).addClassName('available_maps_update');
        window.setTimeout(function(){$('available_maps').removeClassName('available_maps_update');}, 2000);
    }
});
